package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Result;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    public Result save(@RequestBody User user) {
        if(userService.save(user)>=1){
              return   new Result(1000,"保存成功");
        }
            return new Result(2000,"保存失败");
    }
    @PutMapping
    public Result update(@RequestBody User user){
        if (userService.update(user)>=1){
            return new Result(1001,"更新成功");
        }
        return new Result(2001,"更新失败");
    }
    @DeleteMapping("{uuid}")
    public Result save(@PathVariable Integer uuid){
        if (userService.delete(uuid)>=1){
            return new Result(1001,"删除成功");
        }
        return new Result(2001,"删除失败");
    }
    @GetMapping("{uuid}")
    public Result get(@PathVariable Integer uuid){
        User user = userService.get(uuid);
        return new Result(1000,user);
    }
    @GetMapping("{page}/{size}")
    public Result gerAll (@PathVariable Integer page,@PathVariable Integer size){
        PageInfo<User> page1 = userService.gerAll(page, size);
        return new Result(1000,page1);
    }
}
